package com.karry.web;

import com.karry.pojo.Question;
import com.karry.service.QuestionService;
import com.karry.service.impl.QuestionServiceImpl;
import com.karry.util.JsonUtil;
import com.karry.util.TextMatchUtil;
import com.karry.vo.ResultInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/question")
public class QuestionServlet extends HttpServlet {

    QuestionService qs = new QuestionServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 得到用户行为 （判断是什么条件查询：标题查询、日期查询、类型查询）
        String actionName = request.getParameter("actionName");
        System.out.println("index_actionName:" + actionName);


        switch (actionName){
            case "customerService":
                customerService(request, response);
                break;
            default:break;
        }
    }

    private void customerService(HttpServletRequest request, HttpServletResponse response) {

        //获取请求
        String info = request.getParameter("info");

        System.out.println("info:" + info);

        //构造返回结果集
        ResultInfo<ArrayList<Question>> resultInfo = new ResultInfo<>();
        int number;
        //字符串转数字，先判断是否能转换
        if (info.chars().allMatch(Character::isDigit)){
            number = Integer.parseInt(info);
            Question question = qs.searchQuestionById(number);
            if (question == null){
                resultInfo.setCode(0);
                resultInfo.setMsg("此问题不存在，建议修改询问文本或者查询'all'返回所有疑问！");
            }else {
                //精确查询
                resultInfo.setCode(2);
                ArrayList<Question> list = new ArrayList<>();
                list.add(question);
                resultInfo.setResult(list);
            }
        }else if ("all".equalsIgnoreCase(info)){
            ArrayList<Question> list = (ArrayList<Question>) qs.searchAllQuestion();
            resultInfo.setCode(1);
            resultInfo.setResult(list);
            resultInfo.setMsg("找到以下结果，请输入序号进行查询。");
        } else {
            //文本匹配查询

            //获取所有结果
            List<Question> list = qs.searchAllQuestion();
            //剔除不相关问题
            TextMatchUtil textMatchUtil = new TextMatchUtil(list, info);
            ArrayList<Question> list1 = (ArrayList<Question>) textMatchUtil.doProcess();
            if (list1 == null || list1.size() == 0){
                resultInfo.setCode(0);
                resultInfo.setMsg("此问题不存在，建议修改询问文本或者查询'all'返回所有疑问！");
            }else {
                resultInfo.setCode(1);
                resultInfo.setResult(list1);
                resultInfo.setMsg("找到以下结果，请输入序号进行查询。");
            }

        }

        System.out.println("查询结果集:" + resultInfo);

        JsonUtil.toJson(response, resultInfo);
    }


}
